package BanyuanClub.Work.Day0308;

import com.sun.tools.javac.Main;

import java.io.*;
import java.util.concurrent.FutureTask;

/**
 * 利用多线程分割文件
 */
public class Text {
    public void threadFile(File file, int n){
        FutureTask[] ftarr = new FutureTask[n];
        long lon = file.length();
        long avg = lon/n;
        byte[] by = new byte[8];
        File[] splitFileArr = new File[n];
        for (int i = 0; i < n; i++) {
            int index = i;
            FutureTask<File> ft = new FutureTask<File>(() -> {
                File splitFile = new File(file.getName().substring
                        (0, file.getName().lastIndexOf("."))
                        + (index + 1) + ".txt");
                splitFileArr[index] = splitFile;
                try (BufferedInputStream bis = new BufferedInputStream(
                        new FileInputStream(file));
                     BufferedOutputStream bos = new BufferedOutputStream(
                             new FileOutputStream(splitFile))
                ) {
                    bis.skip(avg * index);
                    long restlen = index < (n - 1) ? avg : lon - avg * (n - 1);
                    while (restlen > 0) {
                        int len = bis.read(by);
                        bos.write(by, 0, (int) (restlen < len ? restlen : len));
                        bos.flush();
                        restlen -= len;
                    }
                }
                return null;
            });
            ftarr[i] = ft;
            for (FutureTask<File> ft1 : ftarr
            ) {
                new Thread(ft1).start();
            }

            for (FutureTask<File> ft2 : ftarr
            ) {
                try {
                    new Thread(ft2).join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        //合并
        BufferedOutputStream bos = null;
        BufferedInputStream[] bisArr = new BufferedInputStream[n];
        try {
        bos = new BufferedOutputStream (new FileOutputStream(file.getName().substring
                (0, file.getName().lastIndexOf("."))
                + "download.txt"));
        for (int i = 0; i < n; i++) {
            bisArr[i] = new BufferedInputStream(new FileInputStream(splitFileArr[i]));
            int len;
            while ((len = bisArr[i].read(by)) != -1){
                bos.write(by,0,len);
                bos.flush();
            }
            splitFileArr[i].delete();
        }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != bos){
                for (BufferedInputStream bis : bisArr
                     ) {
                    try {
                        if (null != bis) {
                            bis.close();
                         }
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }


    public static void main(String[] args) {
        Text t = new Text();
        t.threadFile(new File("aaa.txt"),5);

    }
}
